<?php
// import($GLOBALS['spConfig']['controller_path'].'/base.php');
class bobing extends base
{

	public function __construct(){ // 公用
		parent::__construct(); // 这是必须的

		// $this->user_id = 2;
		// $this->checklogin();


		// if($this->user_id==2){
		// 	$this->user_id=8;
		// } 

		
		$this->arrBase = array(
			'user_id'=>$this->user_id,
			'status'=>1,
		);

		$this->tit = '博饼';
		$this->tpl = 'bobing';
		$this->bobing_gameObj = D('bobing_game');
		$this->bobing_logObj = D('bobing_log');
		$this->bobing_userObj = D('bobing_user');
		$this->bobing_zhuliObj = D('bobing_zhuli');
		$this->userObj = D('user');
		$this->roomObj = D('room');
		$this->adObj = D('ad');
		$this->giftObj = D('gift');
		$this->gift_logObj = D('gift_log');
		$this->userObj = D('user');
		$this->bobingObj = D('bobing');

		$this->score_logObj = D('score_log');
		$this->money_logObj = D('money_log');

		$day_zhuli = $this->bobing['day_zhuli'];

		// $this->room_id = arg('room_id');
		// $this->room = $this->roomObj->findById($this->room_id);

		$cond = "status=1 and user_id='{$this->user_id}' and company_id='{$this->company['id']}' and room_id='{$this->room['id']}'";
		$this->bobing_user = $this->bobing_userObj->find(array($cond));

		// dump($this->bobing);exit;

	}

	public function index(){
		$this->current = 'index';
		$this->title = $this->bobing['title'];
		
		$cond = "status=1 and company_id = '{$this->company['id']}'";
		$this->ads = $this->adObj->findAll(array($cond),'orderby desc');
		$this->rooms = $this->roomObj->findAll(array($cond),'orderby desc');
	}

	public function index1(){
		$this->current = 'index';
		$this->title = $this->bobing['title'];
		
		$cond = "status=1 and company_id = '{$this->company['id']}'";
		$this->ads = $this->adObj->findAll(array($cond),'orderby desc');
		$this->rooms = $this->roomObj->findAll(array($cond),'orderby desc');
	}


	public function room(){
		$this->current = 'index';

		//博饼奖池
		$time_s = date('Y-m-d');
		$time_e = date('Y-m-d',strtotime('+1 day'));
		$cond = "status=1 and room_id='{$this->room['id']}' and add_time>'{$time_s}'";
		$this->bobing_game = $this->bobing_gameObj->find(array($cond));
		$money_top1 = $this->bobing_game['money'] * 0.15;
		$money_top1 = round($money_top1,2);
		$this->money_top1 = $money_top1;

		//倒计时
		$this->time_djs = strtotime($time_e) - time();

		// $room_id = arg('room_id');
		$company_id = $this->company['id'];
		$room_id = $this->room['id'];
		if(!$room_id) $room_id = arg('room_id');
		
		$this->title = $this->bobing['title'];

		//朋友圈助力
		$uid = arg('uid');  //7   //2
		if($uid && $uid!=$this->user_id){  // uid=1  user_id=5
			// dump($uid);exit;
			// 判断新用户
			// 
			// $user_info = $this->userObj->findCount(array($cond));
			
			$time_s = date('Y-m-d');
			$time_e = date('Y-m-d',strtotime('+1 day'));

			//我帮已经助力过比人
			$cond = " f_uid='{$this->user_id}' and room_id='{$room_id}' and (add_time>='{$time_s}' and add_time<'{$time_e}')";
			$zhuli_1 = $this->bobing_zhuliObj->findCount(array($cond));

			//别人帮我5次
			$cond = " user_id='{$this->user_id}' and room_id='{$room_id}' and (add_time>='{$time_s}' and add_time<'{$time_e}')";
			$zhuli_2 = $this->bobing_zhuliObj->findCount(array($cond));

			//判断我帮别人过一次没
			$cond = " f_uid='{$this->user_id}' and user_id='{$uid}' and (add_time>='{$time_s}' and add_time<'{$time_e}')";
			$zhuli_3 = $this->bobing_zhuliObj->findCount(array($cond));
			if($zhuli_1 < $this->bobing['day_zhuli'] && $zhuli_2 < $this->bobing['day_zhuli'] && $zhuli_3 == 0){
				//可以助力
				$arrBase = array(
					'company_id'=>$this->company['id'],
					'room_id'=>$room_id,
					'f_uid'=>$this->user_id,
					'user_id'=>$uid,
					// 'num'=>$this->bobing['zhuli_num'],
					'add_time'=>date('Y-m-d H:i:s'),
					'status'=>1,
				);
				$result = $this->bobing_zhuliObj->create($arrBase);
			}

			
		}


		// //总在线人数
		// $online_user = $this->bobing_userObj->findCount(array("status=1"));
		// $this->online_user = $online_user + 1000;

		// //此包厢博饼次数
		// $cond = "status=1 and company_id='{$company_id}' and room_id='{$room_id}'";
		// $bobing_count = $this->bobing_logObj->findCount(array($cond));
		// $this->bobing_count = $bobing_count + 10000;

		// //礼品次数
		// $gift_count = $this->bobing_logObj->findCount(array("status=1 and gift_id>0"));
		// $this->gift_count = $gift_count + 100;
		// 
		
		//今日博饼分
		$time_s = date('Y-m-d');
		$time_e = date('Y-m-d',strtotime("+1 day"));
		$cond = " status=1 and user_id={$this->user_id} and (add_time>='{$time_s}' and add_time<'{$time_e}')";
		$cond .=" and room_id='{$this->room['id']}' and company_id='{$this->company['id']}'";
		$result = $this->bobing_logObj->findAll(array($cond),'id desc','SUM(score) as score');
		// dump($result);exit;
		$my_score = $result[0]['score'];

		if(!$my_score) $my_score = 0;
		$this->my_score = $my_score;
		// $sql = "select user_id,SUM(score) as score from qc_bobing_log where {$cond} group by user_id order by 2 DESC limit 100";
		// $result = $this->bobing_logObj->query($sql);


		$this->cond = " status=1 and user_id='{$this->user_id}' and company_id='{$company_id}' and room_id='{$room_id}'";
		$time_s = date('Y-m-d');
		$time_e = date('Y-m-d',strtotime('+1 day'));
		//今日房间 被助力次数
		$cond = " f_uid>0 and (add_time>='{$time_s}' and add_time<'{$time_e}') and ".$this->cond;
		$zhuli_count = $this->bobing_zhuliObj->findCount(array($cond));

		//今日房间 系统赠送数量
		$cond = " f_uid=0 and (add_time>='{$time_s}' and add_time<'{$time_e}') and ".$this->cond;
		$result = $this->bobing_zhuliObj->find(array($cond));
		$xitong_num = $result['num'];
		if(!$xitong_num) $xitong_num = 0;

		//今日房间 已博次数
		$cond = " status=1 and (add_time>='{$time_s}' and add_time<'{$time_e}') and ".$this->cond;
		$tday_count = $this->bobing_logObj->findCount(array($cond));
		// dump($cond);exit;
		//分享次数 
		$zhuli_num = $zhuli_count * $this->bobing['zhuli_num'];

		//每房间 原始次数
		$xt_count = $this->bobing['day_num'] - $tday_count;

		//每房间总次数 = 今日总次数 + 助力次数 * 每次数量 + 系统赠送 - 今日已博
		// $count = $this->bobing['day_num'] + $zhuli_num + $xitong_num - $tday_count;


		//今日购买
		$cond = " f_uid=0 and (add_time>='{$time_s}') and ".$this->cond;
		// $result = $this->bobing_zhuliObj->find(array($cond),'id asc','sum(num) as cnum');
		$result = $this->bobing_zhuliObj->find(array($cond),'id desc');

		$goumai_num = $result['num'];
		if(!$goumai_num) $goumai_num = 0;

		// $goumai_num = 0;

		//剩余次数   =  每日系统次数 + 购买次数 - 今日已博
		$count = $this->bobing['day_num'] + $zhuli_num + $goumai_num - $tday_count;
		
		if(!$count) $count = 0;


		//此房间剩余次数
		$this->count_s = $count;
		$this->zhuli_count_s = $this->bobing['day_zhuli'] - $zhuli_count;


		//排行
		$cond = "room_id = '{$this->room['id']}' and bb_score>0";
		$result = $this->bobing_userObj->findAll(array($cond),"bb_score desc,id asc");
		if($result){
			foreach($result as $key=>$list){
			     $r[$list['user_id']] = $key + 1;
			}

			if($r[$this->user_id]){
				$pm = $r[$this->user_id];
			}
			
		}

		if(!$pm) $pm = 0;
		$this->pm = $pm;
		// dump($result);exit;

		//博饼礼品记录
		$cond = "status=1 and room_id='{$this->room['id']}' and score>0 and jieguo>5 and gift_id>0";
		$result = $this->bobing_logObj->findAll(array($cond),"id desc",'*',5);
		if($result){
			$this->bobing_users = $this->userObj->ids($result,'user_id');
			$this->bobing_gifts = $this->giftObj->ids($result,'gift_id');
			foreach ($result as $val) {

				$val['bo_name'] = $this->bobing['bo_name'.$val['jieguo']];
				$val['addtime'] = date('H:i',strtotime($val['add_time']));
				$bobing_logs[] = $val;
			}
			$this->bobing_logs = $bobing_logs;
		}
		
		if($this->user_id==1){
			// dump($this->bobing_users);exit;
		}
		// 
		// 
		//查找助力
		// $time_s = date('Y-m-d');
		// $cond = " user_id = '".$this->user_id."' and uid>0 and (add_time >= '".$time_s." 00:00:01' and add_time <= '".$time_s." 23:59:59' )";

		// $result = $bobing_zhuliObj->findAll($cond,null,"sum(num) as num");
		// $zhuli_num = $result[0]['num'];  //30

		//今日被助力次数
		// $zhuli_count = $bobing_zhuliObj->findCount($cond);
		// $this->zhuli_count = $this->bobing['day_zhuli'] - $zhuli_count;

		//系统助力
		// $time_s = date('Y-m-d');
		// $cond = " user_id = '".$this->user_id."' and uid=0 and (add_time >= '".$time_s." 00:00:01' and add_time <= '".$time_s." 23:59:59' )";
		// $result = $bobing_zhuliObj->find($cond);
		// $xitong_num = $result['num'];
		// if(!$xitong_num){
		// 	$xitong_num = 0;
		// }
		// $zhuli_num = $zhuli_num + $xitong_num;

		//今日已博
		// $time_s = date('Y-m-d');
		// $cond = " user_id = '".$this->user_id."' and company_id ='".$company_id."' and (add_time >= '".$time_s." 00:00:01' and add_time <= '".$time_s." 23:59:59' )";
		// $tday_count = $this->bobing_logObj->findCount($cond);
		// $this->day_count = $this->bobing['day_num'] + $zhuli_num - $tday_count;  //130-

		// //总积分
		// $cond = " user_id = '".$this->user_id."'";
		// $this->info = $this->bobing_userObj->find($cond);

		// //博饼礼品记录
		// $cond = " score > 0 and jieguo > 5 and gift_id > 0";
		// $result = $this->bobing_logObj->spLinker()->findAll($cond,"id desc",null,5);
		
		// if($result){
		// 	$companyObj = spClass("lib_company");
		// 	foreach ($result as $val) {
		// 		$val['company'] = $companyObj->find(array('id' => $val['gift']['company_id'], ) );
		// 		$val['bo_name'] = $this->bobing['bo_name'.$val['jieguo']];
		// 		$val['addtime'] = date('H:i',strtotime($val['add_time']));
		// 		$list[] = $val;
		// 	}
		// 	$this->loglist = $list;
		// }

		// //排行
		// $result = $this->bobing_userObj->findAll(null,"score desc");
		// if($result){
		// 	foreach($result as $key=>$list){
		// 	     $r[$list['user_id']] = $key;
		// 	}
		// 	$this->pm = $r[$this->user_id] + 1;
		// }

		// $this->share_title = $this->bobing['share_title']."【".$this->user['nickname'].": ".$this->user['score']."分,第".$this->pm."名】";
		$this->share_title = "【".$this->user['nickname']."】".$this->bobing['share_title'].' - '.$this->room['name'];
		if($this->room['type']==1){
			$desc = "排名前10";
		}
		if($this->room['type']==2){
			$desc = "超过200分";
		}
		$this->share_desc = "今日奖池{$this->bobing_game['money']}元,{$desc}瓜分奖池,全民博饼,祝您博得大奖.";

		$this->title = $this->room['name'];
	}

	

	//记录
	public function log(){
		$this->title = "博饼记录";

		$arrBase = array('page' => '0000',);

		if($this->tab){
			$arrBase = array_merge($arrBase,array('tab' => $this->tab));
		}
		//搜索
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = spUrl("bobing","log_loaddata", $arrBase);
		
	}

	//列表
	public function log_loaddata(){
		$cond = " status=1";

		switch($this->tab){
			case '1':
				$cond .= " and score > 0 ";
				break;
			case '2':
				$cond .= " and jieguo > 5 ";
				break;
			case '3':
				$cond .= " and gift_id > 0 ";
				break;
			default:
				$cond .= " and score > 0 ";
		}
		
		//状态
		$result = $this->bobing_logObj->spLinker()->spPager($this->spArgs('page', 1), 10)->findAll($cond,'id desc');
		$this->pager = $this->bobing_logObj->spPager()->getPager();

        if($this->pager){  //2页的意思
			if($this->pager['total_page'] < $this->spArgs('page') ) {
	            die('0');
	        }
		}else{
			if($this->spArgs('page')>1) {
	            die('0');
	        }
		}
		if($this->tab == 2){
			if(2 < $this->spArgs('page') ) {
	            die('0');
	        }
		}
		if($result){
			$companyObj = spClass("lib_company");
			foreach ($result as $val) {
				$val['bo_name'] = $this->bobing['bo_name'.$val['jieguo']];
				$val['addtime'] = date('H:i',strtotime($val['add_time']));
				$val['gift_company'] = $companyObj->find(array('id' => $val['gift']['company_id'], ) );
				$list[] = $val;
			}
			$this->list = $list;
		}
		
	}

	//规则
	public function guize(){
		$this->title = "博饼规则";
		$this->current = 'guize';
		$this->guize = $this->company['guize'];
	}

	//排行
	public function rank(){
		$this->title = "博饼分排行";
		$this->current = 'rank';

		if(!$this->tab) $this->tab = 1;

		$cond = " status=1 and room_id='{$this->room['id']}' and company_id='{$this->company['id']}' ";
		// dump($this->room);exit;
		switch($this->tab){
			case '1'://今日
				$time_s = date('Y-m-d');
				$time_e = date('Y-m-d',strtotime("+1 day"));

				$cond .= " and bb_score>0";
				$result = $this->bobing_userObj->findAll(array($cond),'bb_score desc','*',100);
				break;
			case '2'://昨日
				$time_s = date('Y-m-d',strtotime("-1 day"));
				$time_e = date('Y-m-d');

				$cond .= " and score>0 and (add_time>='{$time_s}' and add_time<'{$time_e}')";
				$sql = "select user_id,SUM(score) as bb_score from qc_bobing_log where {$cond} group by user_id order by 2 DESC,user_id asc limit 100";
				$result = $this->bobing_logObj->query($sql);
				break;
			case '3':
			    // $time_s = date("Y-m-d",strtotime("-2 monday"));
			    // $time_e = date("Y-m-d",strtotime("-1 sunday"));
				// $time_s = $this->bobing['sdate'];
				// $time_e = '2017-10-04';
				break;
			case '4':
				$cond .= " and score>0";
				$result = $this->bobing_userObj->findAll(array($cond),'score desc','*',100);
				break;
			default:
				$time_s = date('Y-m-d');
				$time_e = date('Y-m-d',strtotime("+1 day"));
		}
		
		if($result && $result[0]['user_id']>0){
			$i=1;
			foreach ($result as $val) {
				$val['i'] = $i;
				$this->users = $this->userObj->ids($result,'user_id');
				$list[] = $val;
				$i = $i+1;
			}
			$this->list = $list;
		}
		
	}

	public function gift(){

		
	}

	//公司排行
	public function company(){
		$this->title = "公司博饼分排行";

		$cond = " status=1 and company_id='".$this->company_id."' ";

		switch($this->tab){
			case '1':
				$time_s = date('Y-m-d');
				$time_e = date('Y-m-d');
				break;
			case '2':
				$time_s = date("Y-m-d",strtotime("-1 day"));
				$time_e = date("Y-m-d",strtotime("-1 day"));
				break;
			case '3':
				// $time_s = date("Y-m-d",strtotime("-2 monday"));
				// $time_e = date("Y-m-d",strtotime("-1 sunday"));
				$time_s = $this->bobing['sdate'];
				$time_e = $this->bobing['edate'];
				break;
			case '4':
				break;
			default:
				$time_s = date('Y-m-d');
				$time_e = date('Y-m-d');
		}
		if($this->tab==4){
			$result = $this->bobing_userObj->spLinker()->findAll($cond,'score desc',null,50);
		}else{
			$cond .= " and (add_time >= '".$time_s." 00:00:01' and add_time <= '".$time_e." 23:59:59' )";
			$sql = "SELECT user_id,SUM(score) as score FROM pp_bobing_log where ".$cond." GROUP BY user_id ORDER BY 2 DESC limit 50";
			$result = $this->bobing_logObj->findSql($sql);
		}
		
		if($result){
			$i=1;
			foreach ($result as $val) {
				$val['i'] = $i;
				$val['user'] = spClass('lib_user')->find(array('id' => $val['user_id'] ));
				$list[] = $val;
				$i = $i+1;
			}
			$this->list = $list;
			// dump($list);exit;
		}
		
	}
	
	//每日排名
	public function rank_day(){
		$this->title = "每日排名";

		$time_s = date("Y-10-01");
		for($i=1; $i <= 7; $i++){
			$day = $i-1;
			$daylist[$i] = date("10-d",strtotime("$time_s +{$day} day"));
        }
        $this->daylist = $daylist;
        

		$cond = " status=1 ";

		if($this->tab){
			$day = $this->tab-1;
			$time_s = date("Y-10-d",strtotime("$time_s +{$day} day"));
		}
		// dump($time_s);exit;
		$this->title = "每日排名".$time_s;
		if(!$this->tab){
			$result = $this->bobing_userObj->spLinker()->findAll($cond,'score desc',null,1000);
		}else{
			$cond .= " and (add_time >= '".$time_s." 00:00:01' and add_time <= '".$time_s." 23:59:59' )";
			$sql = "SELECT user_id,SUM(score) as score FROM pp_bobing_log where ".$cond." GROUP BY user_id ORDER BY 2 DESC limit 100";
			$result = $this->bobing_logObj->findSql($sql);
		}
		
		if($result){
			$i=1;
			foreach ($result as $val) {
				$val['i'] = $i;
				$val['user'] = spClass('lib_user')->find(array('id' => $val['user_id'] ));
				$list[] = $val;
				$i = $i+1;
			}
			$this->list = $list;
			// dump($list);exit;
		}
		
	}







	//助力记录
	public function zhuli_log(){
		$this->title = "助力记录";

		$arrBase = array('page' => '0000',);

		if($this->tab){
			$arrBase = array_merge($arrBase,array('tab' => $this->tab));
		}
		//搜索
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = spUrl("bobing","zhuli_log_loaddata", $arrBase);
		
	}

	//助力列表
	public function zhuli_log_loaddata(){
		$cond = " status=1";

		switch($this->tab){
			case '1':
				$cond .= " and user_id='".$this->user_id."' and uid>0";
				break;
			case '2':
				$cond .= " and uid='".$this->user_id."'";
				break;
			default:
				$cond .= " and user_id='".$this->user_id."' and uid>0";
		}
		
		//状态
		$zhuli_logObj = spClass("lib_bobing_zhuli");
		$result = $zhuli_logObj->spLinker()->spPager($this->spArgs('page', 1), 20)->findAll($cond,'id desc');
		$this->pager = $zhuli_logObj->spPager()->getPager();

        if($this->pager){  //2页的意思
			if($this->pager['total_page'] < $this->spArgs('page') ) {
	            die('0');
	        }
		}else{
			if($this->spArgs('page')>1) {
	            die('0');
	        }
		}
		if($result){
			$bobing_userObj = spClass("lib_bobing_user");
			foreach ($result as $val) {
				$val['addtime'] = date('m-d H:i',strtotime($val['add_time']));
				$val['u_score'] = $bobing_userObj->find(array('user_id' => $val['uid'], ) );
				$val['user_score'] = $bobing_userObj->find(array('user_id' => $val['user_id'], ) );
				$list[] = $val;
			}
			$this->list = $list;
		}
		
	}





	//
	function stat(){

		$this->time_e = date('Y-m-d',strtotime("-1 day"));
		$cond = "status=1 and room_id='{$this->room['id']}'";
		$stat = $this->bobingObj->find(array($cond));
		$this->stat = $stat;

		//博饼奖池
		$time_s = date('Y-m-d');
		$time_e = date('Y-m-d',strtotime('+1 day'));
		$cond = "status=1 and room_id='{$this->room['id']}' and add_time>'{$time_s}'";
		$this->bobing_game = $this->bobing_gameObj->find(array($cond));
		// $money_top1 = $this->bobing_game['money'] * 0.15;
		// $money_top1 = round($money_top1,2);
		// $this->money_top1 = $money_top1;
		// 
		//排行
		//排行奖励60%
		//
		$rank_money = $this->bobing_game['money'] * 0.6;
		$this->rank_money = $rank_money;

		if($this->room['type']==1){

			$rank_lv = array(
				'1'=>0.5,
				'2'=>0.25,
				'3'=>0.1,
				'4'=>0.05,
				'5'=>0.05,
				'6'=>0.01,
				'7'=>0.01,
				'8'=>0.01,
				'9'=>0.01,
				'10'=>0.01,
			);

			$cond = "status=1 and room_id = '{$this->room['id']}' and bb_score>0";
			$result = $this->bobing_userObj->findAll(array($cond),"bb_score desc");
			if($result){
				foreach($result as $key=>$list){
				     $r[$list['user_id']] = $key + 1;
				}
				if($r[$this->user_id]){
					$pm = $r[$this->user_id];
					if($pm<=10){
						$j_money = $rank_money * $rank_lv[$pm];
						$j_money = round($j_money,2);
					}
				}
				
			}

			if(!$pm) $pm = 0;
			$this->pm = $pm;

			if(!$j_money) $j_money = 0;
			$this->j_money = $j_money;

		}

		if($this->room['type']==2){

			$cond = "status=1 and room_id = '{$this->room['id']}' and bb_score>=200";
			$dd_user = $this->bobing_userObj->findCount(array($cond));
			$this->dd_user = $dd_user;

			$j_money = $rank_money / $dd_user;
			$j_money = round($j_money,2);

			if(!$j_money) $j_money = 0;
			$this->j_money = $j_money;

		}

		//积分分红
		$rank_score = $this->bobing_game['money'] * 0.2;
		$this->rank_score = $rank_score;

		
		$score_lv = $this->bobing_user['score'] / $this->stat['score'];
		$j_money2 = $rank_score * $score_lv;
		$j_money2 = round($j_money2,2);

		if(!$j_money2) $j_money2 = 0;
		$this->j_money2 = $j_money2;


	}



















	//博饼
	function run(){

		$room_id = arg('room_id');
		$company_id = $this->company['id'];

		$room_id = $this->room['id'];
		

		if(!$this->user['subscribe'] && $this->bobing['yes_gz']){

			$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->accessToken.'&openid='.$this->user['open_id'].'&lang=zh_CN';
    		$curl = D('Curl',1);
			$info = $curl->get($url);
			$info = json_decode($info,true);

			if($info['subscribe']==0){
				$data['status'] = 0;
				$data['msg'] = '请先关注公众号参与,<br/>谢谢支持，祝您博大奖.';
				$data['link'] = '/index/my/weixin?room_id='.$room_id;
				$this->ajaxReturn($data);
			}else{
				$arrBase = array(
	                'subscribe' =>$info['subscribe'],
	            );
	            $this->userObj->update(array('id'=>$this->user_id),$arrBase);
			}
		}

		//博饼用户
		$cond = "status=1 and user_id='{$this->user_id}' and company_id='{$company_id}' and room_id='{$room_id}'";
		$bobing_user = $this->bobing_userObj->find(array($cond));
		//插入用户
		if(!$bobing_user){

			$arrBase = array(
				'company_id'=> $this->company['id'],
				'room_id'=> $room_id,
				'user_id'=> $this->user_id,
				't_id'=> $this->user['t_id'],
				// 'score'=> $this->bobing['s_score'],
				// 'score'=>0,
				// 'num'=> $this->bobing['zong_num'],
				'status'=> 1,
				'add_time'=>date('Y-m-d H:i:s'),
			);
			$this->bobing_userObj->create($arrBase);
			$data['status'] = 0;
			$data['msg'] = '博饼游戏报名成功,祝您博得状元.';

			// $this->score_logObj->add_log($room_id,$this->user_id, 10, "博饼报名增加");
			// 	//上级奖励积分
			// if($this->user['t_id']){
			// 	$t_user = $this->userObj->findById($this->user['t_id']);
			// 	if($t_user){
			// 		$this->score_logObj->add_log($room_id,$t_user['id'],1,'好友博饼报名(抽成)');
			// 	}
			// }
			$this->ajaxReturn($data);
		}
		if($bobing_user['status']==0){
			$data['status'] = 0;
			$data['msg'] = '暂停博饼资格,请联系客服.';
			$this->ajaxReturn($data);
		}
		
		//完善手机
		if(!$this->user['tel'] && $this->bobing['yes_tel']){
			//博饼礼品记录
			// $gift_logObj = spClass("lib_gift_log");
			// $gift_log = $gift_logObj->find(array('user_id' => $this->user_id, ) );
			// if($gift_log){
				$data['status'] = 0;
				$data['msg'] = '请完善姓名手机,方便兑换领取奖品.谢谢.';
				$data['link'] = '/index/my/setting?room_id='.$room_id;
				$this->ajaxReturn($data);
			// }
		}



		$nowdate = date('Y-m-d H:i:s');
		if($nowdate < $this->bobing['sdate'].' 08:00:00'){
			$data['status'] = 0;
			$data['msg'] = '活动于'.$this->bobing['sdate'].' 08:00开始,<br />感谢关注,敬请期待.';
			$this->ajaxReturn($data);
		}

		if($nowdate > $this->bobing['edate']){
			$data['status'] = 0;
			$data['msg'] = '活动已结束,谢谢关注.';
			$this->ajaxReturn($data);
		}
		

		$this->cond = "status=1 and user_id='{$this->user_id}' and company_id = '{$company_id}' and room_id='{$room_id}'";

		//今日房间 被助力次数
		$time_s = date('Y-m-d');
		$time_e = date('Y-m-d',strtotime('+1 day'));

		//今日房间 被助力次数
		$cond = " f_uid>0 and (add_time>='{$time_s}' and add_time<'{$time_e}') and ".$this->cond;
		$zhuli_count = $this->bobing_zhuliObj->findCount(array($cond));

		//今日房间 系统赠送数量
		$cond = " f_uid=0 and (add_time>='{$time_s}' and add_time<'{$time_e}') and ".$this->cond;
		$result = $this->bobing_zhuliObj->find(array($cond));
		$xitong_num = $result['num'];
		if(!$xitong_num) $xitong_num = 0;

		//今日房间 已博次数
		$cond = " status=1 and (add_time>='{$time_s}' and add_time<'{$time_e}') and ".$this->cond;

		$tday_count = $this->bobing_logObj->findCount(array($cond));

		$zhuli_num = $zhuli_count * $this->bobing['zhuli_num'];

		//每房间 原始次数
		$xt_count = $this->bobing['day_num'] - $tday_count;

		//每房间总次数 = 今日总次数 + 助力次数 * 每次数量 + 系统赠送 - 今日已博
		// $count = $this->bobing['day_num'] + $zhuli_num + $xitong_num - $tday_count;
		// 
		//今日房间 系统赠送数量
		$cond = " f_uid=0 and (add_time>='{$time_s}') and ".$this->cond;
		$result = $this->bobing_zhuliObj->find(array($cond),'id desc');
		$goumai_num = $result['num'];
		if(!$goumai_num) $goumai_num = 0;

		// $goumai_num = 0;

		$count = $this->bobing['day_num'] + $zhuli_num + $goumai_num - $tday_count;

		
		
		if($count <= 0 && $zhuli_count < $this->bobing['day_zhuli']){
			$data['status'] = 0;
			$zhuli_count2 = $this->bobing['day_zhuli'] - $zhuli_count;
			$data['msg'] = '今日博饼次数已用完,可以分享邀请朋友助力,还剩 '.$zhuli_count2." 次机会";
			$this->ajaxReturn($data);
		}

		if($count <= 0){
			$data['status'] = 0;
			$data['msg'] = '今日博饼次数已用完,可以重新报名.';
			$data['link'] = '/index/my/buy?room_id='.$room_id;
			$this->ajaxReturn($data);
		}

		// if($count <= 0){
		// 	$data['status'] = 0;
		// 	$data['msg'] = '今日此包厢总次数已用完,可以更换包厢再博.';
		// 	$this->ajaxReturn($data);
		// }


		//博饼开始
		$num1 = rand(1,6);
		$num2 = rand(1,6);
		$num3 = rand(1,6);
		$num4 = rand(1,6);
		$num5 = rand(1,6);
		$num6 = rand(1,6);

		// if($this->user_id == 2 || $this->user_id == 4){
		// 	$num_arr = array(4,4,4,4,4,4,4,1,2,3,4,5,6);
		// 	$num = rand(0,13);
		// 	$num1 = $num_arr[rand(0,13)];
		// 	$num2 = $num_arr[rand(0,13)];
		// 	$num3 = $num_arr[rand(0,13)];
		// 	$num4 = $num_arr[rand(0,13)];
		// 	$num5 = $num_arr[rand(0,13)];
		// 	$num6 = $num_arr[rand(0,13)];
		// }


		$num_arr = array($num1,$num2,$num3,$num4,$num5,$num6 );
		$num_arr2 = array_count_values($num_arr);


		// dump($num_arr);
		// dump($num_arr2);
		if(!$num_arr2['1']) $num_arr2['1']=0;
		if(!$num_arr2['2']) $num_arr2['2']=0;
		if(!$num_arr2['3']) $num_arr2['3']=0;
		if(!$num_arr2['4']) $num_arr2['4']=0;
		if(!$num_arr2['5']) $num_arr2['5']=0;
		if(!$num_arr2['6']) $num_arr2['6']=0;

		switch($num_arr2['4']){
			case '0':  
				$jieguo = 0;
				if(in_array('4', $num_arr2)) $jieguo = 3;  //有4个一样  四进
				if(in_array('5', $num_arr2)) $jieguo = 7;  //有5个一样  五子登科
				if(in_array('6', $num_arr2) && !$num_arr2['1']) $jieguo = 10;  //有6个一样 不包含6个1   六杯黑
				if(in_array('6', $num_arr2) && $num_arr2['1']) $jieguo = 11;  //有6个1   遍地锦
				break;
			case '1':
				$jieguo = 1;   //一秀
				if($num_arr2['1']==1 && $num_arr2['2']==1 && $num_arr2['3']==1 && $num_arr2['5']==1 && $num_arr2['6']==1) $jieguo = 5;  //123456顺子  对堂
				if(in_array('4', $num_arr2)) $jieguo = 3;  //有4个一样    四进
				if(in_array('5', $num_arr2)) $jieguo = 8;  //有5个一样  五子带一秀
				break;
			case '2':
				$jieguo = 2;   //二举
				if(in_array('4', $num_arr2)) $jieguo = 3;  //有4个一样
				break;
			case '3':
				$jieguo = 4;  //三红
				break;
			case '4':
				$jieguo = 6;   //四红
				if($num_arr2['1']==2) $jieguo = 13;  //状元插金花
				break;
			case '5':
				$jieguo = 9; //五王
				break;
			case '6':
				$jieguo = 12;   //六勃红
				break;
		}
		
		// dump('1'.$num_arr2['1']);
		// dump('2'.$num_arr2['2']);
		// dump('3'.$num_arr2['3']);
		// dump('4'.$num_arr2['4']);
		// dump('5'.$num_arr2['5']);
		// dump('6'.$num_arr2['6']);

		// dump('jieguo:'.$jieguo);
		// exit;

		
		$data['type'] = 2;

		if($jieguo){
			$resultname = '恭喜你博得:<br />'.$this->bobing['bo_name'.$jieguo];
			$score = $this->bobing['bo_score'.$jieguo];
		}else{
			$resultname = '好可惜~没能博中！<br />再接再厉!';
			$score = 0;
		}
		//提示
		$data['img'] = '/public/bobing/image/ok.png';
		$data['resultname'] = $resultname;
		$data['credits'] = $score;
		//骰子
		$data['resultarr'][0] = $num1;
		$data['resultarr'][1] = $num2;
		$data['resultarr'][2] = $num3;
		$data['resultarr'][3] = $num4;
		$data['resultarr'][4] = $num5;
		$data['resultarr'][5] = $num6;
		
		
		//礼品
		$data['prize'] = '';


		//礼品概率
		$gift_chuxian = 0;
		if($this->bobing['gift_gailv']){
			$gailv_arr = explode(',', $this->bobing['gift_gailv']);
			$gailv = array_rand($gailv_arr);
			$gailv = intval($gailv);

			if($gailv > 0){
				$gift_chuxian = 1;
			}
		}else{
			$gift_chuxian = 1;
		}

		$gift_id = 0;

		if($this->user_id  && $jieguo && $this->bobing['yes_gift'] && $gift_chuxian){
			//查找对应的奖品
			$cond = " status=1 and num>0 and find_in_set('{$jieguo}',bobing)";
			$gift_info = $this->giftObj->findAll(array($cond),"rand()",'*',1);
			
			if($gift_info){
				// 存在礼品
				$gift = $gift_info[0];
				$status=0;
				if($gift['hongbao']){
					$status=1;
				}

				if($gift) $gift_id = $gift['id'];
				
				//减少库存
				$this->giftObj->decr(array('id'=>$gift_id), 'num');
				//增加销量
				$this->giftObj->incr(array('id'=>$gift_id), 'sell_num');

				//礼品日志
				$arrBase = array(
					'company_id'=>$company_id,
					'user_id'=>$this->user_id,
					'gift_id'=>$gift_id,
					'num'=>1,
					'add_time'=>date('Y-m-d H:i:s'),
					'status'=>$status,
					
				);
				$this->gift_logObj->create($arrBase);


				$data['prize'] = $gift['name'];
				$data['gift_company'] = $gift['company'];

				if($gift['hongbao']){
					//微信红包start
					// import("WxPayPubHelper/WxPayPubHelper.php");
					require(APP_DIR.'/protected/include/WxPayPubHelper/WxPayPubHelper.php');
					$Redpack = new Redpack_pub();
					$Redpack->setOpenid($this->user['open_id']);
					
					$randomFloat = randomFloat(1,2);
					$randomFloat = round($randomFloat,2);
					$randomFloat = 1;
					$total = $randomFloat * 100;
					$num = 1;

					$data['prize'] = $gift['name'].$randomFloat.'元';

					// //商户订单号
					$Redpack->setParameter('mch_billno', order_number());
					// //商户名称
					$Redpack->setParameter('send_name', $gift['company']);
					//付款金额
					$Redpack->setParameter('total_amount', $total);
					//红包发放总人数
					$Redpack->setParameter('total_num', $num);
					// $Redpack->setParameter('amt_type','ALL_RAND');
					//红包祝福语
					$Redpack->setParameter('wishing', "感谢您的支持，祝您中秋节快乐！");
					//活动名称
					$Redpack->setParameter('act_name', "中秋节快乐！");
					//备注
					$Redpack->setParameter('remark', "博饼红包福利");
					$result = $Redpack->sendRedpack();


					file_put_contents("test.txt", $this->bobing['title']."\r\n", FILE_APPEND);
					foreach ($result as $key => $value) {
						file_put_contents("test.txt", date("Y-m-d H:i:s").":".$key.":".$value."\r\n", FILE_APPEND);
					}
					
					if($result['result_code'] == 'SUCCESS'){
						$status = 1;
					}else{
						$status = 0;
					}
					$arrBase = array(
						'company_id' => $gift['company_id'],
						'user_id' => $this->user_id,
						'gift_id' => $gift['id'],
						'intro' => $result['return_msg'],
						'money' => $randomFloat,
						'add_time' => date("Y-m-d H:i:s"),
						'status' => $status,
					);
					D('hongbao_log')->create($arrBase);
					// dump($result);
					//微信红包end
				}
			}
			//库存减少
			//$giftObj = spClass("lib_gift");
			//$giftObj->decrField(array('id'=>$gift_id), 'num');
			
		}
		$data['code'] = 1;
		//积分次数
		$data['count'] = $count - 1;  //今天还剩
		$data['u_count'] = tday_count - 1;   //已博
		// $data['u_credits'] = $bobing_user['score'] + $score - $this->bobing['score'];  //总积分
		$data['u_credits'] = $bobing_user['bb_score'] + $score;  //总积分

		//写入数据
		$this->arrBase = array(
			'company_id'=>$company_id,
			'room_id'=>$room_id,
			'user_id'=>$this->user_id,
			'num1'=>$num1,
			'num2'=>$num2,
			'num3'=>$num3,
			'num4'=>$num4,
			'num5'=>$num5,
			'num6'=>$num6,
			'jieguo'=>$jieguo,
			'score'=>$score,
			'add_time'=>date('Y-m-d H:i:s'),
			'status'=>1,
			'gift_id'=>$gift_id,
		);
		$result = $this->bobing_logObj->create($this->arrBase);
		if($result){
			//增加博饼分
			$cond = "status=1 and room_id='{$room_id}' and user_id='{$this->user_id}'";
			$this->bobing_userObj->incr(array($cond), 'bb_score',$score);
			//减少次数
			// $this->bobing_userObj->decr(array('user_id'=>$this->user_id), 'num');

			//状元奖励站内积分
			if($jieguo>5){
				$this->score_logObj->add_log($room_id,$this->user_id, 10, "博得状元增加");
				//上级奖励积分
				if($this->user['t_id']){
					$t_user = $this->userObj->findById($this->user['t_id']);
					if($t_user){
						$this->score_logObj->add_log($room_id,$t_user['id'],1,'好友博得状元(抽成)');
					}
				}
			}
			

			//总排行
			$cond = "room_id = '{$room_id}' and bb_score>0";
			$result = $this->bobing_userObj->findAll(array($cond),"bb_score desc,id asc");
			if($result){
				foreach($result as $key=>$val){
				     $r[$val['user_id']] = $key + 1;
				}
				if($r[$this->user_id]){
					$pm = $r[$this->user_id];
				}
			}
			// dump($result);exit;
			// if(!$pm) $pm = 0;
			if($pm && $pm<50){
				//排名
				$data['pm'] = $pm;
			}
			
			// $data['pmdesc'] = "当前排名:".$pm;
			$data['status'] = 1;
		}else{
			$data['status'] = 0;
		}

		$this->ajaxReturn($data);
		
	}


	
}